﻿using System;

namespace mki.Backup.Helper.Extensions
{
	public static class Int64Xtension
	{
		#region Fields (1)

		private const Int64 terabyte = 1099511627776;
		private const Int64 gigabyte = 1073741824;
		private const Int64 megabyte = 1048576;
		private const Int64 kilobyte = 1024;

		#endregion Fields

		#region Methods (5)

		// Public Methods (5) 

		/// <summary>
		/// caluclate size of the specified bytes.
		/// </summary>
		/// <param name="bytes">The bytes.</param>
		/// <returns></returns>
		public static String ToAutoSize(this Int64 bytes)
		{
			if (bytes > terabyte)
			{
				return ToTBSize(bytes);
			}
			if (bytes > gigabyte)
			{
				return ToGBSize(bytes);
			}
			if (bytes > megabyte)
			{
				return ToMBSize(bytes);
			}
			if (bytes > kilobyte)
			{
				return ToKBSize(bytes);
			}
			return bytes + " Bytes";
		}

		/// <summary>
		/// Converts value to GB.
		/// </summary>
		/// <param name="bytes">The bytes.</param>
		/// <returns></returns>
		public static String ToGBSize(this Int64 bytes)
		{
			return (bytes / gigabyte).ToString("0.00 GB");
		}

		/// <summary>
		/// Converts value to KB.
		/// </summary>
		/// <param name="bytes">The bytes.</param>
		/// <returns></returns>
		public static String ToKBSize(this Int64 bytes)
		{
			return (bytes / kilobyte).ToString("0.00 KB");
		}

		/// <summary>
		/// Converts value to MB.
		/// </summary>
		/// <param name="bytes">The bytes.</param>
		/// <returns></returns>
		public static String ToMBSize(this Int64 bytes)
		{
			return ((bytes / megabyte)).ToString("0.00 MB");
		}

		/// <summary>
		/// Converts value to TB.
		/// </summary>
		/// <param name="bytes">The bytes.</param>
		/// <returns></returns>
		public static String ToTBSize(this Int64 bytes)
		{
			return (bytes / terabyte).ToString("0.00 TB");
		}

		#endregion Methods
	}
}